package com.kittlen.managementSystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.kittlen.managementSystem.entity.Choice;
import com.kittlen.managementSystem.entity.Coach;
import com.kittlen.managementSystem.entity.MySqlLimit;
import com.kittlen.managementSystem.entity.Student;
import com.kittlen.managementSystem.mapper.StudentMapper;
import com.kittlen.managementSystem.service.IChoiceService;
import com.kittlen.managementSystem.service.ICoachService;
import com.kittlen.managementSystem.service.IStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kittlen.managementSystem.utils.IdWorker;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 学员表 服务实现类
 * </p>
 *
 * @author kittlen
 * @since 2021-01-26
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {
    @Resource
    IChoiceService iChoiceService;

    @Resource
    ICoachService iCoachService;

    private IdWorker idWorker=new IdWorker(1,1,1);

    @Override
    public List<Student> selects(Wrapper wrapper, MySqlLimit mySqlLimit) {
        return baseMapper.selects(wrapper,mySqlLimit);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateByCoach(Student student, String coachId, String choiceId) throws Exception {
        try {
            this.updateById(student);
            iChoiceService.update(new UpdateWrapper<Choice>().set("coach_id",coachId).eq("choice_id",choiceId));
        }catch (Exception e){
            throw e;
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateAndSaveChoice(Student student, String coachId) throws Exception {
        try {
            Choice choice=new Choice();
            choice.setCoach(new Coach(coachId));
            choice.setStudent(new Student(student.getStudentId()));
            choice.setChoiceId(idWorker.nextId()+"");
            choice.setStage(student.getStudyStatus());
            choice.setStageEnd(false);
            iChoiceService.save(choice);
            Coach coach=iCoachService.getById(coachId);
            //当前学员数已经达到上限
            if(coach.getChoiceCount()==coach.getChoiceCountMax()){
                return false;
            }
            coach.setChoiceCount(coach.getChoiceCount()+1);
            iCoachService.updateById(coach);//修改教练的当前学员人数
            student.setStudyType(0);
            this.updateById(student);
        }catch (Exception e){
            throw e;
        }
        return true;
    }

    /**
     * 学员批量添加
     * @param list
     * @return
     */
    @Override
    public int batchInsert(List<Student> list) {
        return baseMapper.batchInsert(list);
    }
}
